package com.gzhryc.net_marketing.cloud.goods.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.cloud.goods.dao.GoodsDao;
import com.gzhryc.net_marketing.cloud.goods.dao.db.Goods;
import com.gzhryc.net_marketing.cloud.goods.dao.db.GoodsSpace;
import com.gzhryc.net_marketing.cloud.goods.dao.enums.GoodsEnum;
import com.gzhryc.net_marketing.cloud.goods.services.dto.GoodsSearch;
import com.gzhryc.net_marketing.core.models.GoodsSpaceItem;

public class GoodsService {

	static Logger log = Logger.getLogger(GoodsService.class);

	public static GoodsService self() {
		return MultiDBTools.getService(GoodsService.class, true);
	}

	public static GoodsService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey, GoodsService.class, true);
	}

	GoodsDao dao;

	public GoodsService(String jdbcKey) {
		dao = new GoodsDao(jdbcKey);
	}

	public List<Goods> findBySearch(GoodsSearch search, Integer pageNo, Integer pageLimit) {
		Conditions condition = dao.buildConditions(search);
		condition.desc("createDate");
		try {
			return dao.findByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}
	
	public List<Goods> findJoinBySearch(GoodsSearch search, Integer pageNo, Integer pageLimit) {
		Conditions condition = dao.buildConditions(search);
		condition.desc("createDate");
		try {
			return dao.findJoinByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	public Long countBySearch(GoodsSearch search) {
		Conditions condition = dao.buildConditions(search);
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
		return 0L;
	}
	
	public Long countJoinBySearch(GoodsSearch search) {
		Conditions condition = dao.buildConditions(search);
		try {
			return dao.countJoin(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
		return 0L;
	}

	public Long countBySupplierId(Long supplierId){
		if(supplierId != null){
			Conditions conditions = new Conditions(Goods.class);
			conditions.and().is("supplierId",supplierId);
			try {
				return dao.count(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return 0L;
	}

	public Goods getById(Long id) {
		if (id != null) {
			try {
				return dao.getById(id);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}
	

	public Goods getJoinById(Long id) {
		if (id != null) {
			try {
				return dao.getJoinById(id);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public Goods getByCode(String code) {
		if (StringTools.isNotBlank(code)) {
			Conditions conditions = new Conditions(Goods.class);
			conditions.and().is("code",code);

			try {
				return dao.get(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public Goods getByName(String name) {
		if (StringTools.isNotBlank(name)) {
			Conditions condition = new Conditions(Goods.class);
			condition.and().is("name", name);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public boolean add(Goods goods) throws LogicException {
		if (StringTools.isNotBlank(goods.getName())) {
			Goods temp = getByName(goods.getName());
			if (temp != null) {
				throw new LogicException("商品名称已存在");
			}
			goods.setCreateDate(new Date());
			try {
				if (dao.insert(goods) > 0) {
					return true;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return false;
	}

	public boolean add(Goods goods, List<GoodsSpaceItem> spaceItemList) throws LogicException {
		if (StringTools.isNotBlank(goods.getName())) {
			Goods temp = getByName(goods.getName());
			if (temp != null) {
				throw new LogicException("商品名称已存在");
			}
			goods.setCreateDate(new Date());

			boolean flag = false;
			JdbcSession.begin();
			try {
				if (dao.insert(goods) > 0) {
					if (StringTools.isNotBlank(goods.getCode()) && goods.getSupplierId() != null) {
						flag = true;
						if (spaceItemList != null && spaceItemList.size() > 0) {
							for (GoodsSpaceItem item : spaceItemList) {
								GoodsSpace goodsSpace = new GoodsSpace();
								goodsSpace.setGoodsId(goods.getId());
								goodsSpace.setId(item.getId());
								goodsSpace.setName(item.getName());
								goodsSpace.setThumbnailImg(item.getThumbnailImg());
								goodsSpace.setStockNum(item.getStockNum());
								goodsSpace.setState(item.getState());
								if (!GoodsSpaceService.self(this.dao.getJdbcKey()).add(goodsSpace)) {
									flag = false;
									break;
								}
							}
						}
						if (flag) {
							JdbcSession.commit();
							return true;
						}
					} else {
						JdbcSession.commit();
						return true;
					}
				}
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			} finally {
				JdbcSession.end();
			}
		}
		return false;
	}

	public boolean updateNotNull(Goods goods) throws LogicException {
		if (StringTools.isNotBlank(goods.getName())) {
			Goods temp = getByName(goods.getName());
			if (temp != null && !temp.getId().equals(goods.getId())) {
				throw new LogicException("商品名称已存在");
			}
		}
		try {
			return dao.updateNotNull(goods) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
		return false;
	}

	public boolean upGoods(Long id) throws LogicException{
		Goods goods = getById(id);
		if(goods == null){
			throw new LogicException("未找到商品信息");
		}

		if(GoodsEnum.State.PutOnTheShelf.index().equals(goods.getState())){
			throw new LogicException("商品已上架");
		}

		Goods updateEntity = new Goods();
		updateEntity.setId(id);
		updateEntity.setState(GoodsEnum.State.PutOnTheShelf.index());

		try {
			if(dao.updateNotNull(updateEntity) > 0){
				return true;
			}
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean downGoods(Long id) throws LogicException{
		Goods goods = getById(id);
		if(goods == null){
			throw new LogicException("未找到商品信息");
		}

		if(GoodsEnum.State.OffTheShelf.index().equals(goods.getState())){
			throw new LogicException("商品已下架");
		}

		Goods updateEntity = new Goods();
		updateEntity.setId(id);
		updateEntity.setState(GoodsEnum.State.OffTheShelf.index());

		try {
			if(dao.updateNotNull(updateEntity) > 0){
				return true;
			}
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean deleteById(Long id) throws LogicException {
		Goods temp = getById(id);
		if (temp == null) {
			throw new LogicException("商品不存在");
		}
		JdbcSession.begin();
		try {
			if(dao.deleteById(id) > 0){
				if(GoodsSpaceService.self(this.dao.getJdbcKey()).deleteByGoodsId(id)){
					if(GoodsEvaluateService.self(this.dao.getJdbcKey()).deleteByGoodsId(id)) {
						JdbcSession.commit();
						return true;
					}
				}
			}
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		} finally {
			JdbcSession.end();
		}
		return false;
	}
}
